<?php

if (!defined('BASEPATH'))
    exit('No direct script access allowed');

require_once(APPPATH . '/controllers/super_admin' . EXT);
class Lesson extends Super_admin {

    function __construct() {
        parent::__construct();

        $this->load->model(array(
            'Group_model',
            'User_model',
            'Lesson_group_model',
            'Lesson_model',
            'Lesson_file_model',
        ));

        $this->load->helper(array(
            'security',
            'string',
            'download',
        ));
    }
    /**
     * list_records
     *
     * Load information list
     *
     * @access public
     * @param int
     * @return void
     */
    function list_records($group_id) {
        $access = array('student', 'instructor');
        $this->_is_authorize($access); //check super_admin controller

        $data['group'] =  $this->Group_model->get_record(array('where' => 'group_id = "'.$group_id.'"'));

        $this->load->view('lesson/list',$data);
    }
    function get_lessons() {

        $search = $this->input->post('search', TRUE);
        $group_id = $this->input->post('group_id', TRUE);

        $where = 'l.lesson_id != "!@#"';

        $join_lesson_groups = array(
            'table' => LESSONS_GROUPS . ' lg',
            'condition' => 'lg.lesson_id = l.lesson_id AND lg.group_id = "'.$group_id.'"',
        );

        //Search by
        $filter = 'l.lesson_name LIKE "' .$search.'%" ';  //lesson name

        $where .= ( ! empty($search) ) ? ' AND  '.$filter : NULL;

        $select = 'l.lesson_id, l.lesson_name, lg.group_id';

        $order_by = $this->input->post('sidx') . ' ' . $this->input->post('sord');

        $args = array(
                    'select' => $select,
                    'where' =>  $where,
                    'join' => array($join_lesson_groups),
                    'order_by' => $order_by,
                    'group_by' => 'l.lesson_id'
        );

        //For pagination
        $page = $this->input->post('page');
        $total = count($this->Lesson_model->get_list($args));

        $num_pages = ceil($total / LIMIT);

        if ($page > $num_pages && $total <> 0) {
            $page = $num_pages;
        }

        $offset = (LIMIT * $page) - LIMIT;

        //PUSH THIS VALUE INTO ARGS
        $args['limit'] = LIMIT;
        $args['offset'] = $offset;
        $args['resource_id'] = TRUE;
        //end Pagination

        $records = $this->Lesson_model->get_list($args);

        $jsn_records = '[]';
        if ($total > 0) {
            $jsn_records = $this->json_formatter->_run($records, 'lesson_id');
        }

        echo '{"page": "' . $page . '", "total": "' . $num_pages . '", "records": "' . $total . '", "rows": ' . $jsn_records . '}';
    }

    function get_lessons_files() {
        $lesson_id = $this->input->post('lesson_id', TRUE);

        $where = 'lf.lesson_id = "'.$lesson_id.'"';

        $select = 'lf.lesson_file_id, lf.file_name';

        $order_by = $this->input->post('sidx') . ' ' . $this->input->post('sord');

        $args = array(
                    'select' => $select,
                    'where' =>  $where,
                    'order_by' => $order_by,
        );

        //For pagination
        $page = $this->input->post('page');
        $total = count($this->Lesson_file_model->get_list($args));

        $num_pages = ceil($total / LIMIT);

        if ($page > $num_pages && $total <> 0) {
            $page = $num_pages;
        }

        $offset = (LIMIT * $page) - LIMIT;

        //PUSH THIS VALUE INTO ARGS
        $args['limit'] = LIMIT;
        $args['offset'] = $offset;
        $args['resource_id'] = TRUE;
        //end Pagination

        $records = $this->Lesson_file_model->get_list($args);

        $jsn_records = '[]';
        if ($total > 0) {
            $jsn_records = $this->json_formatter->_run($records, 'lesson_file_id');
        }

        echo '{"page": "' . $page . '", "total": "' . $num_pages . '", "records": "' . $total . '", "rows": ' . $jsn_records . '}';
    }

    function add($group_id = 0){
        $access = array('instructor');
        $this->_is_authorize($access); //check super_admin controller

        $data = array(
            'lesson_id' => 0,
            'lesson_name' => '',
            'group_id' => $group_id,
        );
        
        $this->load->view('lesson/form', $data);
        
    }
    
    function edit($group_id = 0, $id = 0){
        $access = array('instructor');
        $this->_is_authorize($access); //check super_admin controller
        
        $params = array(
                'select' => 'l.*',
                'where' => array('lesson_id' => $id),
                'join' => array(),
                'order_by' => NULL
        );

        $data = $this->Lesson_model->get_record($params);
        $data['group_id'] = $group_id;

        $this->load->view('lesson/form', $data);
        
    }
    
    function view($group_id, $id){
        $access = array('student', 'instructor');
        $this->_is_authorize($access); //check super_admin controller
        
        $params = array(
                'select' => 'l.*',
                'where' => array('l.lesson_id' => $id),
                'order_by' => NULL
        );

        $data = $this->Lesson_model->get_record($params);
        $data['group_id'] = $group_id;

        $this->load->view('lesson/display', $data);
    }

    function lesson_files($group_id = 0, $id = 0){
        $access = array('instructor');
        $this->_is_authorize($access); //check super_admin controller

        $params = array(
                'select' => 'l.*',
                'where' => array('lesson_id' => $id),
                'join' => array(),
                'order_by' => NULL
        );

        $data = $this->Lesson_model->get_record($params);
        $data['group_id'] = $group_id;

        $this->load->view('lesson/lessonfiles', $data);

    }

    function lesson_download_files($group_id = 0, $id = 0){
        $access = array('student');
        $this->_is_authorize($access); //check super_admin controller

        $params = array(
                'select' => 'l.*',
                'where' => array('lesson_id' => $id),
                'join' => array(),
                'order_by' => NULL
        );

        $data = $this->Lesson_model->get_record($params);
        $data['group_id'] = $group_id;

        $this->load->view('lesson/lessonfiles_student', $data);

    }

    function download_file($id) {

        $where = 'lesson_file_id =' . $id;
        $file_name = $this->Lesson_file_model->get_value('file_name', $where);

        $data = file_get_contents(LESSONS_FOLDER.$file_name); // Read the file's contents

        return force_download($file_name, $data);

    }

    function upload_files()
    {
        $upload_config['upload_path'] = LESSONS_FOLDER;
        $upload_config['allowed_types'] = '*';
        $upload_config['encrypt_name'] = FALSE;
        $upload_config['remove_spaces'] = TRUE;
        $upload_config['overwrite'] = FALSE;
        $upload_config['max_size'] = '2000';
        $image_data = NULL;

        $this->load->library('upload');
        $this->upload->initialize($upload_config);

        $group_id = $this->input->post('group_id', TRUE);
        $lesson_id = $this->input->post('lesson_id', TRUE);
     
        if ( ! empty($_FILES['file_name']['name']) ) {
            if ( ! $this->upload->do_upload('file_name'))
            {
                _error(sprintf(lang('cms_custom'), join('<br />', $this->upload->error_msg)), FALSE);
                return redirect('lesson/Lesson_files/'.$group_id.'/'.$lesson_id);
            }

            $file_data = $this->upload->data();
        }

        $data_lesson_files = array(
            'lesson_id' => $lesson_id
            ,'file_name' => $file_data['file_name']

        );

        $this->Lesson_file_model->_insert($data_lesson_files);

        _success(sprintf(lang('cms_custom'), 'File sucessfully uploaded.'), FALSE);
        return redirect('lesson/Lesson_files/'.$group_id.'/'.$lesson_id);
    }
    
    function submit(){
        
        $response = FALSE;
        $lesson_id = $this->input->post('lesson_id', TRUE);
        $group_id = $this->input->post('group_id', TRUE);

        $data = array(
            'lesson_name' => $this->input->post('lesson_name',TRUE),
        );

        if (empty($lesson_id)) {

            $lesson_id = $this->Lesson_model->_insert($data);
                    
            $data_lesson_group = array(
                'lesson_id' => $lesson_id,
                'group_id' => $group_id
            );

            $this->Lesson_group_model->_insert($data_lesson_group);
            
            $msg = sprintf(lang('cms_add_success'), "Lesson");
            $response = TRUE;

        } else {
 
            $where = array('lesson_id' => $lesson_id);
            
            $data_lesson_group = array(
                'lesson_id' => $lesson_id,
                'group_id' => $group_id
            );

            $this->Lesson_model->_update($where, $data);
            
            $this->Lesson_group_model->_update($where, $data_lesson_group);

            $msg = sprintf(lang('cms_edit_success'), "Lesson");

            $response = TRUE;
        }

        _success($msg, FALSE);
        echo json_encode($response);
    }
    
    function delete($id) {
        $access = array('instructor');
        $this->_is_authorize($access); //check super_admin controller

        _success(sprintf(lang('cms_delete_success'), "Lesson"));

        $data = $this->Lesson_file_model->get_list(array(
            'select' => '*',
            'where' => array('lesson_id' => $id)
        ));

        //delete lesson files
        foreach ($data as $d) {
            if (! empty($d['file_name'])) unlink(LESSONS_FOLDER.$d['file_name']);
            $this->Lesson_file_model->_delete(array('lesson_file_id' => $d['lesson_file_id']));
        }

        $this->Lesson_model->_delete(array('lesson_id' => $id));

        $msg = sprintf(lang('cms_delete_success'), "Lesson");

        $response = TRUE;
        _success($msg, FALSE);
        echo json_encode($response);
    }

    function delete_file($id) {
        $access = array('instructor');
        $this->_is_authorize($access); //check super_admin controller

        _success(sprintf(lang('cms_delete_success'), "Lesson file"));

        $data = $this->Lesson_file_model->get_record(array(
            'select' => 'file_name',
            'where' => array('lesson_file_id' => $id)
        ));

        if (! empty($data['file_name'])) unlink(LESSONS_FOLDER.$data['file_name']);

        $this->Lesson_file_model->_delete(array('lesson_file_id' => $id));

        $msg = sprintf(lang('cms_delete_success'), "Lesson file");

        $response = TRUE;
        _success($msg, FALSE);
        echo json_encode($response);
    }
}
/* End of file lesson_file.php */
/* Location: ./application/controllers/lesson_file.php */
